package com.adinnet.api.peaseRecord.controller;


import com.adinnet.api.child.model.Child;
import com.adinnet.api.child.service.ChildService;
import com.adinnet.api.common.model.AuthUtil;
import com.adinnet.api.peaseRecord.model.PeaseRecord;
import com.adinnet.api.peaseRecord.model.VO.PeaseRecordForm;
import com.adinnet.api.peaseRecord.model.VO.PeaseReq;
import com.adinnet.api.peaseRecord.model.VO.ShareReq;
import com.adinnet.api.peaseRecord.service.PeaseRecordService;
import com.adinnet.api.user.model.User;
import com.adinnet.common.utils.ResponseUtil;
import com.adinnet.core.BizResult;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResp;
import com.adinnet.core.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 豌豆记录表 前端控制器
 * </p>
 *
 * @author chenjia
 * @since 2020-12-02
 */
@RestController
@RequestMapping("api/peaseRecord")
public class PeaseRecordController {

    @Autowired
    private PeaseRecordService peaseRecordService;

    /**
     *根据儿童id查询金豆记录
     * @param childId 宝宝id
     * @return
     */
    @GetMapping("getPeaseRecord")
    public JsonResult getPeaseRecord(@RequestParam(value = "childId") Integer childId) {
        try {
            List<PeaseRecord> peaseRecords = peaseRecordService.getPeaseRecords(childId);
            return JsonResult.ok().put("data",peaseRecords);
        }catch (Exception e){
            return JsonResult.error("获取失败");
        }
    }


    /**
     * 收取金豆
     * @return
     */
    @PostMapping("collectPease")
    public JsonResult collectPease(@RequestBody PeaseReq pease) {
        try {
            if(pease.getPeaseId()==null){
                return JsonResult.error("豆豆id不能为空");
            }
            PeaseRecord peaseRecord = peaseRecordService.getById(pease.getPeaseId());
            if(peaseRecord.getStatus()==1){
                return JsonResult.error("操作失败,金豆已收获!");
            }
            return peaseRecordService.collectPease(pease.getPeaseId());
        }catch (Exception e){
            return JsonResult.error("操作失败");
        }
    }

    /**
     * 收获豆藤
     * @return
     */
    @PostMapping("collectRattan")
    public JsonResult collectRattan(@RequestBody PeaseReq pease) {
        try {
            if(pease.getChildId()==null){
                return JsonResult.error("儿童id不能为空！");
            }
            return peaseRecordService.collectRattan(pease.getChildId());
        }catch (Exception e){
            return JsonResult.error("操作失败");
        }
    }

    /**
     * 金豆豆藤明细
     * @return
     */
    @PostMapping("getPeaseRecordList")
    public JsonResult getPeaseRecordList(@RequestBody PeaseRecordForm form) {
        try {
            if(form.getChildId()==null){
                return JsonResult.error("儿童id不能为空");
            }
            if(form.getType()==null){
                form.setType(1);//默认查金豆
            }
            if(form.getStatus()==null){
                form.setStatus(0);//默认查全部
            }
            return peaseRecordService.getPeaseRecordList(form);
        }catch (Exception e){
            return JsonResult.error("查询失败");
        }
    }

    /**
     * 分享
     *
     * @return
     */
    @PostMapping("share")
    public JsonResult share(@Valid @RequestBody ShareReq shareReq) {
        try {
            return peaseRecordService.share(shareReq);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("分享失败");
        }
    }

    /**
     * 极光测试
     *
     * @return
     */
    @GetMapping("testJg")
    public JsonResult testJg(@RequestParam(value = "childId") Integer childId) {
        try {
            peaseRecordService.testJg(childId);
            return JsonResult.ok("操作成功!");
        } catch (Exception e) {
            return JsonResult.error("清除失败！");
        }
    }
}
